package com.nbx.app.zeuscmdport;


import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;

/**
 * http://www.zeuscmd.com/tutorials/opengles/10-Transformations.php
 * 
 * @author 5109200610
 *
 */
public class Render10 extends RenderBase {
	
	public Render10(Context context) {
		super(context);
	}

	private float xrot = 0.0f;
	private float yrot = 0.0f;

	private float triangle[] = {
			-0.25f, -0.25f, 0.0f, 
			0.25f, -0.25f, 0.0f,
			-0.25f, 0.25f, 0.0f};

	private float square[] = {
			-0.25f, -0.25f, 
			0.25f, 	-0.25f, 
			-0.25f, 0.25f,
			0.25f, 0.25f};

	private float colors[] = {
			1.0f, 0.0f, 0.0f, 1.0f, 
			0.0f, 1.0f, 0.0f, 1.0f,
			0.0f, 0.0f, 1.0f, 1.0f};


	private FloatBuffer mTriangleBuffer;
	private FloatBuffer mSquareBuffer;
	private FloatBuffer mColorsBuffer;

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// preparation
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		mTriangleBuffer = createFloatBuffer(triangle);
		mSquareBuffer = createFloatBuffer(square);
		mColorsBuffer = createFloatBuffer(colors);
		
		gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f);

	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();

		gl.glViewport(0, 0, width, height);
		gl.glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	public void onDrawFrame(GL10 gl) {
		gl.glClear (GL10.GL_COLOR_BUFFER_BIT);

		// Triangle
		gl.glShadeModel(GL10.GL_SMOOTH);

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mTriangleBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorsBuffer);

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

		gl.glPushMatrix();
			gl.glTranslatef(0.25f, 0.5f, 0.0f);
			gl.glScalef(0.5f, 0.5f, 0.5f);
			gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);
			gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
		gl.glPopMatrix();

			gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

		// Square
		gl.glShadeModel(GL10.GL_FLAT);

		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mSquareBuffer);
		
		gl.glColor4f(0.25f, 0.25f, 0.75f, 1.0f);

		gl.glPushMatrix();
			gl.glTranslatef(0.75f, 0.5f ,0.0f);
			gl.glScalef(0.5f, 0.5f, 0.5f);
			gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);
			gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		gl.glPopMatrix();
		
		xrot += 2.0f;
		yrot += 3.0f;
	}
}